// table.cc 
//	Routines to managing object table.
//

#include "table.h"
#include "debug.h"


Table::Table(int size)
{
	ASSERT(size>0);
	
	table = new void*[size];
	
	for (int i = 0;i<size;i++)
	{
		table[i] = NULL;
	}
	tableSize = size;
	numItem = 0;
}

Table::~Table()
{
	delete []table;
}

int Table::Alloc(void *obj)
{
	for (int i = 0;i<tableSize;i++)
	{
		if (table[i] == NULL)
		{
			table[i] = obj;
			++numItem;
			return i;
		}
	}
	cerr << "Table is full " << endl;
	//ASSERTNOTREACHED();
	return (-1);
}

void* Table::Get(int index)
{
	if (index<0 || index>=tableSize)
		return NULL;
	return table[index];   //Retrieve the object or NULL
}

void Table::Release(int index)
{
	if (index<0 || index>=tableSize)
		return ;
	table[index] = NULL;   // Free the table slot at index.
	--numItem;
}

	